package com.xing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xing.core.BaseServiceImpl;
import com.xing.dao.ApplyDao;
import com.xing.dao.AuthenticationDao;
import com.xing.dto.AuthenticationDTO;
import com.xing.entity.ApplyEntity;
import com.xing.entity.AuthenticationEntity;
import com.xing.properties.SecurityProperties;
import com.xing.service.AuthenticationService;
import com.xing.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/4/19 21:15
 */
@Service
@Slf4j
public class AuthenticationServiceImpl extends BaseServiceImpl<AuthenticationDao, AuthenticationEntity> implements AuthenticationService {
    @Autowired
    private AuthenticationDao dao;
    @Autowired
    private ApplyDao applyDao;

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public boolean isAuthenticated(AuthenticationDTO auth) {
        AuthenticationEntity entity = new AuthenticationEntity().entity(auth);
        log.info("user:{}",entity);
        try {
            dao.insert(entity);
            User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            ApplyEntity applyEntity = new ApplyEntity(principal.getId(),entity.getId(),new Date());
            applyDao.insert(applyEntity);
        }catch (Exception e) {
            System.out.println(2);
            log.info("Error insertinge:{}", e);
            return false;
        }
        return true;
    }
}
